package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.pipe.controller.dto.MarkerQuery;
import com.deyuanyun.pic.pipe.controller.vo.merge.MarkerMergeVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.MarkerVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.WarningVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.MarkerService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * “埋地标识”控制类(包含 埋地标识 和 警示带、警示板)
 *
 * @ClassName MarkerController
 * @author 荣华兴
 * @date 2015年8月13日
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/pipe/marker")
public class MarkerController extends PipeBaseController {

	@Resource
	private MarkerService markerService;

	/**
	 * 跳转到 埋地标识 主页面
	 *
	 * @return
     */
	@RequestMapping("/toMarker")
	public ModelAndView toMarker() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/basedata/pipe/markerMgr");
		return mav;
	}

	/**
	 * 根据条件分页 合并查询 埋地标识（包含埋地标识 和 警示带、警示板）
	 *
	 * @param markerQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(MarkerQuery markerQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		jsonBean.setList(new MarkerMergeVO().fromDomainList(markerService.queryMarkerMerge(markerQuery)));
		AjaxSupport.sendSuccessText("success", jsonBean);
	}

	/**
	 * 根据id查询 埋地标识 详情
	 *
	 * @param id
	 */
	@RequestMapping("/findMarker.json")
	@ResponseBody
	public void findMarker(String id) {
		MarkerVO markerVO = new MarkerVO();
		markerVO.fromDomain(markerService.findMarker(id));
		AjaxSupport.sendSuccessText("success", markerVO);
	}
	/**
	 * 根据id查询 警示带、警示板 详情
	 *
	 * @param id
	 */
	@RequestMapping("/findWarning.json")
	@ResponseBody
	public void findWarning(String id) {
		WarningVO warningVO = new WarningVO();
		warningVO.fromDomain(markerService.findWarning(id));
		AjaxSupport.sendSuccessText("success", warningVO);
	}

	/**
	 * 根据条件 导出“埋地标识”
	 *
	 * @param markerQuery
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public void export(MarkerQuery markerQuery) throws Exception{

		Page<MarkerVO> markerList = new MarkerVO().fromDomainList(markerService.queryMarker(markerQuery));
		Page<WarningVO> warningList = new WarningVO().fromDomainList(markerService.queryWarning(markerQuery));

		// 埋地标识
		List<ExcelUtil.SheetParam> list = new ArrayList<ExcelUtil.SheetParam>();
		String[] columnKeys =  {"pipeLineName", "markerNumber", "manufacturedNum", "mileage", "reference", "referenceDistance", "markerType",
				"manufacturer", "installDate", "assemblyCompany", "markerDepth", "recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(columnKeys, BeanConverter.toListMap(markerList), 2));
		// 警示带、警示板
		String[] lineColumnKeys = {"pipeLineName", "markerNumber", "fromMileage", "fromReference", "fromReferenceDistance",
				"toMileage", "toReference", "toReferenceDistance", "markerType", "recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(lineColumnKeys, BeanConverter.toListMap(warningList), 2));

		PipeExportEnum pipeExportEnum = PipeExportEnum.Marker;
		DownExcelUtil.export(pipeExportEnum.getFileName(), pipePath+ pipeExportEnum.getTempPath(), list);
	}
}
